package newxml;

import com.jme.animation.AnimationController;
import com.jme.animation.Bone;
import com.jme.animation.BoneAnimation;
import com.jme.animation.SkinNode;
import com.jme.app.SimpleGame;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import com.jme.scene.Spatial;
import com.jme.util.BoneDebugger;
import com.jme.util.resource.ResourceLocatorTool;
import com.jme.util.resource.SimpleResourceLocator;
import com.jmex.model.collada.ColladaImporter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import jmetest.renderer.loader.TestColladaLoading;

public class TestXML_InOut_1 extends SimpleGame{
    public static void main(String[] args) {
        TestXML_InOut_1 app = new TestXML_InOut_1();
        app.setDialogBehaviour(ALWAYS_SHOW_PROPS_DIALOG);
        app.start();
    }

    private Spatial loadCollada(){
        Node ret = new Node("collada scene");
        try {
            ResourceLocatorTool.addResourceLocator(
                    ResourceLocatorTool.TYPE_TEXTURE,
                    new SimpleResourceLocator(TestColladaLoading.class
                            .getClassLoader().getResource(
                                    "jmetest/data/model/collada/")));
        } catch (URISyntaxException e1) {
            Logger.getLogger("").warning("Unable to add texture directory to RLT: "
                    + e1.toString());
        }
        
        //this stream points to the model itself.
        InputStream mobboss = TestColladaLoading.class.getClassLoader()
                .getResourceAsStream("jmetest/data/model/collada/man.dae");
        //this stream points to the animation file. Note: You don't necessarily
        //have to split animations out into seperate files, this just helps.
        InputStream animation = TestColladaLoading.class.getClassLoader()
        .getResourceAsStream("jmetest/data/model/collada/man_walk.dae");
        if (mobboss == null) {
            Logger.getLogger("").info("Unable to find file, did you include jme-test.jar in classpath?");
            System.exit(0);
        }
        //tell the importer to load the mob boss
        ColladaImporter.load(mobboss, "model");
        //we can then retrieve the skin from the importer as well as the skeleton
        SkinNode sn = ColladaImporter.getSkinNode(ColladaImporter.getSkinNodeNames().get(0));
        Bone skel = ColladaImporter.getSkeleton(ColladaImporter.getSkeletonNames().get(0));
        //clean up the importer as we are about to use it again.
        ColladaImporter.cleanUp();
        
        //load the animation file.
        ColladaImporter.load(animation, "anim");
        //this file might contain multiple animations, (in our case it's one)
        ArrayList<String> animations = ColladaImporter.getControllerNames();
        if(animations != null) {
	        Logger.getLogger("").info("Number of animations: " + animations.size());
	        for(int i = 0; i < animations.size(); i++) {
	            Logger.getLogger("").info(animations.get(i));
	        }
	        //Obtain the animation from the file by name
	        BoneAnimation anim1 = ColladaImporter.getAnimationController(animations.get(0));
	        
	        //set up a new animation controller with our BoneAnimation
	        AnimationController ac = new AnimationController();
	        ac.addAnimation(anim1);
	        ac.setRepeatType(Controller.RT_WRAP);
	        ac.setActive(true);
	        ac.setActiveAnimation(anim1);
	        
	        //assign the animation controller to our skeleton
	        skel.addController(ac);
        }
        
        //attach the skeleton and the skin to the rootnode. Skeletons could possibly
        //be used to update multiple skins, so they are seperate objects.
        ret.attachChild(sn);
        ret.attachChild(skel);
        ret.updateGeometricState(0, true);
        
        //all done clean up.
        ColladaImporter.cleanUp();
        return ret;
    }
    
    @Override
    protected void simpleInitGame() {
        try {
            final com.jme.scene.Node n = new com.jme.scene.Node("my <\"test\"> & &lt; node");
            n.attachChild(loadCollada());
            
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            new XMLExporter().save(n, bos);
            bos.flush();
            bos.close();
            final byte[] bytes = bos.toByteArray();
            
            FileOutputStream fos = new FileOutputStream("out.xml");
            fos.write(bytes);
            fos.close();
            
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            
            final Node n2 = (Node) new XMLImporter().load(bis);
//            new XMLExporter().save(n2, System.out);
            rootNode.attachChild(n2);
            
//            JMETreeModel.showScenegraph(rootNode);
        } catch (Exception ex) {
            Logger.getLogger(TestXML_InOut.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    protected void simpleRender() {
        super.simpleRender();
        BoneDebugger.drawBones(rootNode, display.getRenderer(), true);
    }
}
